package com.huiyun.project.im.core.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huiyun.project.common.data.cache.RedisUtils;
import com.huiyun.project.common.sequence.IdUtils;
import com.huiyun.project.im.common.auth.AppPrincipal;
import com.huiyun.project.im.common.auth.SecurityUtils;
import com.huiyun.project.im.consts.ImCsSessionConst;
import com.huiyun.project.im.consts.ImServerConst;
import com.huiyun.project.im.consts.ImTenantConst;
import com.huiyun.project.im.core.dto.ImTenantDto;
import com.huiyun.project.im.core.dto.ImTenantMessageDto;
import com.huiyun.project.im.core.dto.ImUserDto;
import com.huiyun.project.im.core.mapper.ImTenantMapper;
import com.huiyun.project.im.core.service.IImCsSessionService;
import com.huiyun.project.im.core.service.IImTenantService;
import com.huiyun.project.im.core.service.IImUserService;
import com.huiyun.project.im.entity.ImCsSession;
import com.huiyun.project.im.entity.ImTenant;
import com.huiyun.project.im.entity.ImUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * IM租户 服务实现类
 * </p>
 *
 * @author bogon
 * @since 2020-07-18
 */
@Service
public class ImTenantServiceImpl extends ServiceImpl<ImTenantMapper, ImTenant> implements IImTenantService {
    /**
     * 新增或修改租户
     * 同一个 appgroup下 相同名称只有一个
     *
     * @param imTenants
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<ImTenantDto> saveOrUpdateTenant(List<ImTenant> imTenants) {
        AppPrincipal appPrincipal = SecurityUtils.getAppPrincipal();
        Date date = new Date();
        List<ImTenantDto> result = new ArrayList<>();
        ImTenantDto tenantDto;
        for (ImTenant imTenant : imTenants) {
            tenantDto = new ImTenantDto();
            //查询是否已存在租户
            ImTenant db = getByAppGroupIdAndBid(appPrincipal.getAppGroupId(), imTenant.getBid());
            if (null == db) {
                imTenant.setId(String.valueOf(IdUtils.getId()));
                imTenant.setCreateBy(appPrincipal.getId().startsWith(ImServerConst.APP_STORE_PREFIX) ? appPrincipal.getId().split(":")[1] : appPrincipal.getId());
                imTenant.setAppGroupId(appPrincipal.getAppGroupId());
                imTenant.setCreateTime(date);
                //新增im租户
                this.save(imTenant);
                BeanUtil.copyProperties(imTenant, tenantDto);
                tenantDto.setAppGroupId(appPrincipal.getAppGroupName());
                result.add(tenantDto);
            } else {
                db.setModifyBy(appPrincipal.getId());
                db.setModifyTime(date);
                db.setLogo(imTenant.getLogo());
                db.setTenantCate(imTenant.getTenantCate());
                db.setName(imTenant.getName());
                //更新租户信息
                this.updateById(db);
                BeanUtil.copyProperties(imTenant, tenantDto);
                tenantDto.setAppGroupId(appPrincipal.getAppGroupName());
                result.add(tenantDto);
            }
        }
        return result;
    }


    /**
     * 通过业务Id和所属应用组可以判断租户是否重复
     *
     * @param appGroupId
     * @param bid
     * @return
     */
    public ImTenant getByAppGroupIdAndBid(String appGroupId, String bid) {
        return this.getOne(Wrappers.<ImTenant>lambdaQuery().eq(ImTenant::getAppGroupId, appGroupId).eq(ImTenant::getBid, bid));
    }


}
